package com.michale.admin.controller.common;

import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Anonymous;
import com.michale.common.domain.permissions.MenuEntity;
import com.michale.common.domain.permissions.UserEntity;
import com.michale.common.result.AjaxResult;
import com.michale.common.utils.redis.RedisUtils;
import com.michale.common.utils.security.SecurityUtils;
import com.michale.framework.security.service.LoginService;
import com.michale.framework.security.service.PermissionService;
import com.michale.system.service.ConfigService;
import com.michale.system.service.MenuService;
import com.michale.system.service.PostService;
import com.michale.system.service.RoleService;
import com.michale.system.vo.login.InfoVo;
import com.michale.system.vo.login.LoginBody;
import com.michale.system.vo.login.RouterVo;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Michale
 * @apiNote 登录验证
 */
@Api(tags = "登录验证")
@RestController
public class LoginController extends BaseController {

    @Resource
    private LoginService loginService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private MenuService menuService;
    @Resource
    private ConfigService configService;
    @Resource
    private RoleService roleService;
    @Resource
    private PostService postService;
    @Resource
    private RedisUtils redisUtils;

    /**
     * 登录接口
     *
     * @param loginBody 登录信息
     * @return 返回token信息
     */
    @PostMapping("/login")
    public AjaxResult login(@Valid @RequestBody LoginBody loginBody) {
        String token = loginService.login(loginBody);
        return AjaxResult.success().put("token", token);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public InfoVo getInfo() {
        InfoVo infoVo = new InfoVo();
        UserEntity userEntity = getLoginUser().getUserEntity();
        List<Long> roleIdList = roleService.selectRoleListByUserId(userEntity.getUserId());
        List<Long> postIdList = postService.selectPostListByUserId(userEntity.getUserId());
        Long[] roleIds = roleIdList.toArray(new Long[roleIdList.size()]);
        Long[] postIds = postIdList.toArray(new Long[roleIdList.size()]);
        userEntity.setPostIds(postIds);
        userEntity.setRoleIds(roleIds);
        infoVo.setUser(userEntity);
        //获取角色数据权限
        infoVo.setRoles(permissionService.getRolePermission(userEntity));
        //获取角色数据权限
        infoVo.setPermissions(permissionService.getMenuPermission(userEntity));
        infoVo.setLogo(configService.selectConfigByKey("system_logo"));
        infoVo.setTitle(configService.selectConfigByKey("system_title"));
        return infoVo;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    public AjaxResult getRouters() {
        List<MenuEntity> menus = menuService.selectMenuTreeByUserId(SecurityUtils.getUserId());
        //构建前端路由所需要的菜单
        List<RouterVo> roleVoList = menuService.buildMenus(menus);
        return AjaxResult.success(roleVoList);
    }

    /**
     * 强制其他设备登录的账号
     *
     * @param userName 用户名
     */
    @Anonymous
    @DeleteMapping("/forceLogout/{userName}")
    public AjaxResult forceLogout(@PathVariable String userName) {
        loginService.forceLogout(userName);
        return AjaxResult.success(403,"账号强制退出成功，请重新登录");
    }
}
